SearchBar = function(CanvasElement) {
  var root = CanvasElement.getHost().content.Root;
  this.hasFocus = false;
  this.searchcategory = "Web";
  this.cursoranim = root.findName("Cursor_blink");
  this.moreanim_in = root.findName("SearchBar_More_In");
  this.moreanim_out = root.findName("SearchBar_More_Out");
  this.isStdTxt = true;
  this.keydown = false;
  this.textCanvas = CanvasElement;
  this.textElement = root.findName("SearchBar_Txt");
  this.textCursor = root.findName("SearchBar_Cursor");
  this.searchButton = root.findName("SearchBar_Btn");
  this.txt = this.textElement.Text;
  this.txt_std = "Powered by Live Search";
  this.textCanvas.addEventListener("MouseLeftButtonDown", this.TextClick);
  this.textElement.addEventListener("MouseLeftButtonDown", this.TextClick);
  this.searchButton.addEventListener("MouseLeftButtonDown", this.BtnClick);
  root.addEventListener("KeyDown", this.ProcessKey);
  root.addEventListener("KeyUp", this.ProcessKeyUp);
}

SearchBar.prototype = {
  TextClick: function(sender, eventArgs) {
      if(SearchBar.hasFocus)return;
      SearchBar.textCursor.opacity = 1;
      SearchBar.cursoranim.begin();
      SearchBar.moreanim_out.stop();
      SearchBar.moreanim_in.begin();
      SearchBar.hasFocus = true;      
      if(SearchBar.txt == SearchBar.txt_std)
        SearchBar.txt = "";
        
      SearchBar.textElement.Text = SearchBar.txt;
  },

  BtnClick: function(sender, eventArgs) {
    var search_str = "";
    if(SearchBar.searchcategory == "Web")search_str = "http://search.live.com/results.aspx?go=Search&form=degad&q=";
    else if(SearchBar.searchcategory == "Videos")search_str = "http://search.live.com/video/results.aspx?go=SearchVideo&form=degad&q=";
    else if(SearchBar.searchcategory == "Bilder")search_str = "http://search.live.com/images/results.aspx?go=SearchImages&form=degad&q=";
    else if(SearchBar.searchcategory == "Maps")search_str = "http://maps.live.de/LiveSearch.LocalLive?go=SearchMaps&form=degad&q=";
    else if(SearchBar.searchcategory == "xRank")search_str = "http://search.live.com/xrank/results.aspx?form=degad&q=";
    else if(SearchBar.searchcategory == "News")search_str = "http://search.live.com/news/results.aspx?go=SearchNews&form=degad&q=";
    re = /" "/g;
    search_str += SearchBar.txt.replace(re,"%20");
    //window.open(search_str);
    search_str += "&ocid=MSN_IGoogle";
    if(SearchBar.txt == "")return;
    _ADS_ClickDestinationUrl(search_str);
    SearchBar.looseFocus(SearchBar.textElement, eventArgs);
  },
  
  looseFocus: function(sender, eventArgs) {
    if(eventArgs.Source.Name == "SearchBar_Bg" ||
      eventArgs.Source.Name == "SearchBar_Txt" ||
      (eventArgs.Source.getParent() != null && 
        eventArgs.Source.getParent().getParent().Name == "SearchBar_More_Container"))return;
    else if(!SearchBar.hasFocus)return;
    SearchBar.hasFocus = false;
    SearchBar.cursoranim.stop();
    SearchBar.moreanim_in.stop();
    SearchBar.moreanim_out.begin();
    SearchBar.textCursor.opacity = 0;
    
    if(SearchBar.txt.length == 0) {
      SearchBar.txt = SearchBar.txt_std;
      SearchBar.textElement.Text = SearchBar.txt;
    }
    
  },
  
  ProcessKey: function(sender, eventArgs) {
    if(!SearchBar.hasFocus)return;
    
    switch(eventArgs.Key) {
    
      case 3: {
        SearchBar.cursoranim.stop();
        SearchBar.textCursor.opacity = 0;
        SearchBar.BtnClick(null, eventArgs);
        break;
      }
      case 1: {
        SearchBar.keydown = true;
        if(SearchBar.txt.length == 0)return;
        else {
          SearchBar.txt = SearchBar.txt.substring(0, SearchBar.txt.length-1);
        }
        break;
      }
      case 9: {
        SearchBar.txt += " ";
        break;
      }
    }
    
    if(eventArgs.Key >= 20 && eventArgs.Key <= 55) {
      if(eventArgs.Shift) {
        SearchBar.txt += String.fromCharCode(eventArgs.PlatformKeyCode);
      }
      else {
        SearchBar.txt += String.fromCharCode(eventArgs.PlatformKeyCode).toLowerCase();
      }
    }
    else if(eventArgs.Key == 255) {
      if(eventArgs.PlatformKeyCode == 186)SearchBar.txt += unescape("%FC");
      else if(eventArgs.PlatformKeyCode == 192)SearchBar.txt += unescape("%F6");
      else if(eventArgs.PlatformKeyCode == 222)SearchBar.txt += unescape("%E4");
    }
    //alert("text: " + SearchBar.txt);
    SearchBar.textElement.Text = SearchBar.txt;
    SearchBar.textCursor["Canvas.Left"] = SearchBar.textElement.ActualWidth + 6;
  },
  
  ProcessKeyUp: function(sender, eventArgs) {
    if(eventArgs.Key == 1) {
      SearchBar.keydown = false;
    }
  }
}
